// 2013-7-31下午10:04:51  net.betou.trade.main.manager.impl.UserGroupMngImpl.java

package net.betou.trade.main.manager.impl;

import java.util.List;

import net.betou.common.hibernate3.Updater;
import net.betou.trade.entity.main.UserGroup;
import net.betou.trade.main.dao.UserGroupDao;
import net.betou.trade.main.manager.UserGroupMng;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class UserGroupMngImpl implements UserGroupMng{
	
	public UserGroup [ ] deleteByIds( Integer [ ] ids ){
		UserGroup [ ] beans = new UserGroup[ ids.length ];
		
		for( int i = 0; i < ids.length; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		
		return beans;
		
	}
	public UserGroup deleteById( Integer id ) {
		return dao.deleteById( id );
	}

	@Transactional( readOnly = true )
	public UserGroup findByDefault( ) {
		return dao.findByDefault( );
	}
	
	@Transactional( readOnly = true )
	public UserGroup findById( Integer id ) {
		return dao.findById( id );
	}
	
	@Transactional( readOnly = true )
	public List< UserGroup > getAllList( String searchProperty, String searchValue, String orderProperty, String orderDirection ) {
		return dao.getList( null, searchProperty, searchValue, orderProperty, orderDirection );
	}

	@Transactional( readOnly = true )
	public List< UserGroup > getAllowReadList( String searchProperty, String searchValue, String orderProperty, String orderDirection ) {
		return dao.getList( true, searchProperty, searchValue, orderProperty, orderDirection );
	}

	public UserGroup save( UserGroup bean ) {
		bean.initToSave( );
		// 如果有选择默认
		if( bean.getGroupDefault( ) ){
			dao.updateToDefault( ) ; // 更新所有的为非默认
		}
		
		return dao.save( bean );
	}

	public UserGroup update( UserGroup bean ) {
		bean.init( );
		
		if( bean.getGroupDefault( ) ){
			dao.updateToDefault( ) ;
		}
		
		Updater< UserGroup > updater = new Updater< UserGroup >( bean );
		updater.exclude( "createTime" );
		return dao.updateByUpdater( updater );
	}

	public int updateToDefault( ) {
		return dao.updateToDefault( );
	}
	
	@Autowired
	private UserGroupDao dao;

}
